package com.lc.cehui.ui.activity.land;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.http.request.GetCodeApi;
import com.lc.cehui.http.request.RegisterApi;
import com.lc.cehui.http.response.CodeBean;
import com.lc.cehui.http.response.RegisterBean;
import com.lc.cehui.manager.InputTextManager;
import com.lc.cehui.util.CourseUtils;
import com.lc.cehui.util.constant.EvenConstant;
import com.lc.cehui.util.eventbus.Event;
import com.lc.cehui.util.eventbus.EventBusUtils;
import com.lc.cehui.widget.CountdownView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterPersonalActivity extends AppActivity {

    @BindView(R.id.nick_name)
    EditText nickName;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.et_vx)
    EditText etVx;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.et_password_agine)
    EditText etPasswordAgine;
    @BindView(R.id.cb_all)
    CheckBox cbAll;
    @BindView(R.id.cdv_yzm)
    CountdownView cdvYzm;
    @BindView(R.id.bt_submit)
    Button btSubmit;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {
        InputTextManager.with(this)
                .addView(nickName)
                .addView(etPhone)
                .addView(etCode)
                .addView(etPassword)
                .addView(etPasswordAgine)
                .addView(etVx)
                .setMain(btSubmit)
                .build();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);

    }

    private void register() {
        if (!etPassword.getText().toString().trim().equals(etPasswordAgine.getText().toString().trim())){
            toast("两次输入密码不一致");
        }
        // 提交注册
        EasyHttp.post(this)
                .api(new RegisterApi()
                        .setCnname_wx(etVx.getText().toString())
                        .setNickname(nickName.getText().toString())
                        .setMobile(etPhone.getText().toString())
                        .setYzm(etCode.getText().toString())
                        .setPassword(etPassword.getText().toString())
                        .setRepassword(etPasswordAgine.getText().toString()))
                .request(new HttpCallback<RegisterBean>(this) {


                    @Override
                    public void onSucceed(RegisterBean data) {
                        toast(data.getMsg());
                        if (data.getCode().equals("200")){
                            EventBusUtils.sendEvent(new Event(EvenConstant.REGISTERFINSH));
                            finish();

                        }
                    }


                });
    }
    @OnClick({R.id.cdv_yzm, R.id.bt_submit, R.id.tv_xy, R.id.tv_ys})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cdv_yzm:
                if (CourseUtils.isNullOrEmpty(etPhone.getText().toString().trim())){
                    toast("请输入手机号");
                    return;
                }
                EasyHttp.post(this)
                        .api(new GetCodeApi()
                                .setMobile(etPhone.getText().toString()))
                        .request(new HttpCallback<CodeBean>(this) {

                            @Override
                            public void onSucceed(CodeBean data) {
                                toast(data.getMsg());
                                if (data.getCode().equals("200")){
                                    cdvYzm.start();
                                }
                            }
                        });
                break;
            case R.id.bt_submit:
                register();
                break;
            case R.id.tv_xy:
                break;
            case R.id.tv_ys:
                break;
        }
    }
}
